<?php
require_once DIR_CLASSES.'common_class.php';
include_once DIR_DB_CLASSES.'db_type.php';





/**
 * Description of Type
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    0.1 Initial version
 */
class Type extends CommonClass{

    private $db;
    private $id;
    private $name;
    private $desc='';
    
    private $name_dbname;
    private $desc_dbname;
   
    
    public function __construct($db_object, $db_name, $db_desc, $id='') {
        
        //parent::__construct();
        
        $this->name_dbname = trim($db_name);
        $this->desc_dbname = trim($db_desc);
        
        $this->db = $db_object;
        if (!is_object($this->db)){
            $this->setError(TXT_ERROR_INVALID_OBJECT_NAME);
        }
        if ($id !== ''){
            
            // Load class vars
            $this->loadType($id);
        }
     
    }
    private function allVarsSet($type=CLASS_VARS_ALL){
        
        switch( $type ){
            case CLASS_VARS_ALL:
                if ( 
                        empty ($this->id) 
                   ) {
                    return FALSE;
                }
                /* Intended fallthrough */
            case CLASS_VARS_MANDATORY_ONLY:
                if ( 
                        empty ($this->name)     ||
                        empty ($this->desc)
                   ) {
                    return FALSE;
                }
                break;
                 
            default:
                /** Oops **/                    
                return FALSE;
        }
        return TRUE;
    }
    
    private function loadType($id){
        // Load class vars from db based on id
        $this->name = $this->db->getName();
        $this->desc = $this->db->getDescription();
        
        // Set class vars and id
    }public function checkName($value){
        $value = trim($value);
        if ( empty($value) ){
            $this->setError("[$value]" );
        }
        if ( !$this->checkNameLen($value) ){
            return FALSE;
        }
    }
    public function setDescription($value){
        $this->desc = $value;
    }
    public function setName($value){
        $this->name = $value;
    }
    
    public function getDescription(){
        return $this->desc;
    }
    public function getName(){
        return $this->name;
    }
    
    
    public function getTypeList(){
        
        $return_list = array();
        
        $list = $this->db->getList(TRUE);
        
        foreach( $list as $idx => $name){
            $return_list[$idx] = $name;
        }
        
        return $return_list;
    }
    /**
     * save()
     * This function saves the class vars (if all mandatory are set)
     * @return bool if FALSE check error array
     */
    public function save(){

        if( $this->allVarsSet(CLASS_VARS_MANDATORY_ONLY) ){
            
            if (!$this->db->save( $this->name, $this->desc)){
                $this->setErrorArray($this->db->getErrorArray() );
                return FALSE;
            }
            
        } else {
            $this->setError( TEXT_CLASS_MISSING_VARS_FOR_SAVE );
            return FALSE;       
        }
        // update Id
        //$this->id = $this->getId();
        return TRUE;
    }

}

class TestType {

    public function TestType() {
        $test = new Type();
    }

}

?>